package com.lvchen.common.view;

import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lvchen.R;

/**
 * Created by 律晨 on 2018/6/14.
 */
public class AppUpdateDialog {
    private Context context;
    private Dialog dialog;
    private RelativeLayout rl_rdp;
    private TextView tv_rdp_title;
    private TextView tv_rdp_content;
    private TextView tv_rdp_cancal;
    private TextView tv_rdp_sure;
    private Display display;
    private boolean showTitle = false;
    private boolean showMsg = false;
    private boolean showPosBtn = false;
    private boolean showNegBtn = false;
    public AppUpdateDialog(Context context) {
        this.context = context;
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
    }
    public AppUpdateDialog builder() {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_commondialog, null);

        rl_rdp = (RelativeLayout) view.findViewById(R.id.rl_rdp);
        tv_rdp_title = (TextView) view.findViewById(R.id.tv_rdp_title);
        tv_rdp_title.setVisibility(View.GONE);
        tv_rdp_content = (TextView) view.findViewById(R.id.tv_rdp_content);
        tv_rdp_content.setVisibility(View.GONE);
        tv_rdp_cancal = (TextView) view.findViewById(R.id.tv_rdp_cancal);
        tv_rdp_cancal.setVisibility(View.GONE);
        tv_rdp_sure = (TextView) view.findViewById(R.id.tv_rdp_sure);
        tv_rdp_sure.setVisibility(View.GONE);
        dialog = new Dialog(context, R.style.AlertDialogStyle);
        dialog.setContentView(view);
        rl_rdp.setLayoutParams(new FrameLayout.LayoutParams((int) (display.getWidth() * 0.85), LinearLayout.LayoutParams.WRAP_CONTENT));
        return this;
    }

    public AppUpdateDialog setTitle(String title, String msg) {
        showTitle = true;
        if ("".equals(title)) {
            tv_rdp_title.setText("提示");
        } else {
            tv_rdp_title.setText(title);
        }
        showMsg = true;
        if ("".equals(msg)) {
            tv_rdp_content.setText("提示");
        } else {
            tv_rdp_content.setText(msg);
        }

        return this;
    }

    public AppUpdateDialog setCancelable(boolean cancel) {
        dialog.setCancelable(cancel);
        return this;
    }

    public AppUpdateDialog setPositiveButton(String text, final View.OnClickListener listener) {
        showPosBtn = true;
        if ("".equals(text)) {
            tv_rdp_sure.setText("确认");
        } else {
            tv_rdp_sure.setText(text);
        }

        tv_rdp_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(v);
                dialog.dismiss();
                dialog = null;
            }
        });
        return this;
    }
    public AppUpdateDialog setNegativeButton(String text, final View.OnClickListener listener) {
        showNegBtn = true;
        if ("".equals(text)) {
            tv_rdp_cancal.setText("取消");
        } else {
            tv_rdp_cancal.setText(text);
        }
        tv_rdp_cancal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(v);
                dialog.dismiss();
                dialog = null;
            }
        });
        return this;
    }

    private void setLayout() {

        if (!showTitle && !showMsg) {
            tv_rdp_title.setVisibility(View.VISIBLE);
            tv_rdp_content.setVisibility(View.VISIBLE);
        }

        if (showTitle) {
            tv_rdp_title.setVisibility(View.VISIBLE);
            tv_rdp_content.setVisibility(View.VISIBLE);
        }

        if (!showPosBtn && !showNegBtn) {
            tv_rdp_sure.setVisibility(View.VISIBLE);
            tv_rdp_sure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
        }

        if (showPosBtn && showNegBtn) {
            tv_rdp_sure.setVisibility(View.VISIBLE);
            tv_rdp_cancal.setVisibility(View.VISIBLE);
        }

        if (showPosBtn && !showNegBtn) {
            tv_rdp_sure.setVisibility(View.VISIBLE);
        }

        if (!showPosBtn && showNegBtn) {
            tv_rdp_cancal.setVisibility(View.VISIBLE);
        }
    }

    public void show() {
        setLayout();
        dialog.show();
    }

    public boolean isShowing(){
        if(dialog != null) {
            return dialog.isShowing();
        } else {
            return false;
        }
    }
}